package com.unohacha.qqj.fzdr.personal.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.superhttp.request.UploadRequest;
import com.qpg.widget.common.ToastUtil;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.ImgBean;
import com.unohacha.qqj.fzdr.ui.activity.PubSettlementActivity;
import com.unohacha.qqj.fzdr.ui.activity.supply.FindJobActivity;
import com.unohacha.qqj.fzdr.util.GlideImageLoaderForPicker;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AuthenticationPersonalActivity extends BaseActivity {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.img_card)
    ImageView imgCard;
    @BindView(R.id.img_info)
    ImageView imgInfo;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    public static final int IMAGE_ITEM_ADD = -1;
    public static final int REQUEST_CODE_SELECT_CARD = 100;
    public static final int REQUEST_CODE_SELECT_INFO = 101;
    public static final int REQUEST_CODE_PREVIEW = 102;
    private String identity_card_positive="",certificate="";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_authentication_personal;
    }

    @OnClick(R.id.common_back)
    void onCommonBackClick() {
        finish();
    }

    @OnClick(R.id.iv_add_id_card)
    void onAddIdCardClick() {
        //打开选择,本次允许选择的数量
        Intent intent1 = new Intent(AuthenticationPersonalActivity.this, ImageGridActivity.class);
        startActivityForResult(intent1, REQUEST_CODE_SELECT_CARD);
   //     Toast.makeText(getApplicationContext(), "添加身份证正面照", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.iv_add_qualification)
    void onAddQualificationClick() {
        Intent intent1 = new Intent(AuthenticationPersonalActivity.this, ImageGridActivity.class);
        startActivityForResult(intent1, REQUEST_CODE_SELECT_INFO);
   //     Toast.makeText(getApplicationContext(), "添加职业资格证书", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.btn_commit)
    void onBtnCommitClick() {
        SuperHttp.post("api/mycenter/authenticationCenter")
                .addParam("token",Prefer.getInstance().getToken())
                .addParam("typeid","1")
                .addParam("identity_card_positive",identity_card_positive)
                .addParam("certificate",certificate)
                .request(new SimpleCallBack<BaseBean<Object>>() {
                    @Override
                    public void onSuccess(BaseBean<Object> data) {
                       ToastUtil.showCustomToast(data.getMessage());
                       if(data.getCode()==1){
                           finish();
                       }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });

      //  Toast.makeText(getApplicationContext(), "提交个人认证", Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void initView() {
//        super.initView();
        commonTitle.setText("个人认证");
        commonRightImage.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {
        super.initData();
        initImagePicker();
    }

    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoaderForPicker());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(false);                           //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(1);              //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }

    ArrayList<ImageItem> images = null;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT_CARD) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
                    Glide.with(this)
                            .load(images.get(0).path)
                            .into(imgCard);
                    imgUpload(images.get(0).path,1);
                }
            }else if(data != null && requestCode == REQUEST_CODE_SELECT_INFO){
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
                    Glide.with(this)
                            .load(images.get(0).path)
                            .into(imgInfo);
                    imgUpload(images.get(0).path,2);
                }
            }
        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                if (images != null) {
//                    selImageList.clear();
//                    selImageList.addAll(images);
//                    imagePickerAdapter.setImages(selImageList);
                }
            }
        }
    }



    private void imgUpload(String path, final int flag){
        HashMap<String,String> parmas=new HashMap<>();
        parmas.put("token", Prefer.getInstance().getToken());
        parmas.put("type","4");
        UploadRequest uploadRequest= SuperHttp.upload("api/filepic/filePic");
        if(path!=null && !path.equals("")){
            uploadRequest.addImageFile("file",new File(path));
        }else {

            return;
        }

        uploadRequest.addParams(parmas);

        showLoading();
        uploadRequest.request(new SimpleCallBack<BaseBean<List<ImgBean>>>() {
            @Override
            public void onFail(int errCode, String errMsg) {
                ToastUtil.showCustomToast(errMsg);
                hideLoading();
            }

            @Override
            public void onSuccess(BaseBean<List<ImgBean>> data) {
                if(data.getCode()==1){
                    if(flag==1){
                        identity_card_positive=data.getData().get(0).getId();
                    }else if(flag==2){
                        certificate=data.getData().get(0).getId();
                    }
                }

                hideLoading();
            }
        });
    }
}
